package com.ly.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.vo.TreeVO;
import com.ly.common.core.constant.BaseConstant;
import com.ly.common.core.utils.BaseUtils;
import com.ly.system.system.bean.dto.DeptTreeSearchDTO;
import com.ly.system.system.bean.entity.SysDept;
import com.ly.system.mapper.SysDeptMapper;
import com.ly.system.service.ISysDeptService;
import com.ly.system.util.TreeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 部门-服务
 *
 * @author jeecg
 * @Since 2019-01-22
 */
@Service
@RequiredArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public R getId(String id) {
        SysDept entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(SysDept entity) {
        List<SysDept> list = this.list(
                new LambdaQueryWrapper<SysDept>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), SysDept::getOrgCode, entity.getOrgCode())
                        .like(StrUtil.isNotBlank(entity.getOrgName()), SysDept::getOrgName, entity.getOrgName())
                        .in(CollUtil.isNotEmpty(entity.getIdList()), SysDept::getId, entity.getIdList())
                        .orderByAsc(SysDept::getOrgCode)
                        .orderByAsc(SysDept::getDisplayOrder)
        );
        return R.ok(list);
    }

    @Override
    public R add(SysDept entity) {
        entity.setOrgCode(IdUtil.getSnowflake(1, 1).nextIdStr());
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(SysDept entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delete(String id) {
        return this.deleteBatch(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatch(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        boolean flag = this.removeByIds(idList);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R getDeptTree(DeptTreeSearchDTO dto) {
        Integer type = dto.getType();
        // 权限信息
        List<String> idList = null;
        if (ObjUtil.equal(1, type)) {
            String deptIds = BaseUtils.getDeptIds();
            if (StrUtil.isEmpty(deptIds)) {
                return R.ok();
            }
            idList = Arrays.asList(deptIds.split(","));
        }
        List<SysDept> list = this.list(
                new LambdaQueryWrapper<SysDept>()
                        .eq(SysDept::getDelFlag, BaseConstant.DEL_FLAG_0.toString())
                        .eq(SysDept::getStatus, BaseConstant.STATUS_1)
                        .like(StrUtil.isNotEmpty(dto.getKeyWord()), SysDept::getOrgName, dto.getKeyWord())
                        .in(CollUtil.isNotEmpty(idList), SysDept::getId, idList)
                        .orderByAsc(SysDept::getDisplayOrder)
        );
        List<TreeVO> vos = TreeUtils.getTreeVoByDeptList(list);
        return R.ok(vos);
    }

    @Override
    public R getDeptTreeSync(DeptTreeSearchDTO dto) {
        String pid = dto.getPid();
        List<SysDept> list = list(
                new LambdaQueryWrapper<SysDept>()
                        .eq(SysDept::getDelFlag, BaseConstant.DEL_FLAG_0.toString())
                        .eq(StrUtil.isNotEmpty(pid), SysDept::getParentId, pid)
                        .and(StrUtil.isEmpty(pid),
                                wq -> wq.isNull(SysDept::getParentId)
                                        .or().eq(SysDept::getParentId, "")
                        )
                        .orderByAsc(SysDept::getDisplayOrder)
        );
        List<TreeVO> vos = TreeUtils.getTreeVoByDeptList(list);
        return R.ok(vos);
    }

    @Override
    public R getDeptTreeVoByIds(String ids) {
        List<SysDept> list = this.list(
                new LambdaQueryWrapper<SysDept>()
                        .in(SysDept::getId, StrUtil.split(ids, ","))
        );
        List<TreeVO> vos = TreeUtils.getTreeVoByDeptList(list, false);
        return R.ok(vos);
    }
}
